unit uRel1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FMTBcd, DB, DBClient, Provider, SqlExpr, Grids, Wwdbigrd,
  Wwdbgrid, StdCtrls, Buttons, ComCtrls, EditDate, ExtCtrls, wwSpeedButton,
  wwDBNavigator, wwclearpanel, wwdbdatetimepicker, DBGrids, JvExDBGrids,
  JvDBGrid, JvComponentBase, JvDBGridExport,ShellApi;

type
  TfRel1 = class(TForm)
    SpAcomProd: TSQLStoredProc;
    sds: TSQLDataSet;
    dsp: TDataSetProvider;
    cds: TClientDataSet;
    DataSource1: TDataSource;
    Panel1: TPanel;
    btnProcurar: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    BitBtn1: TBitBtn;
    wwDBGrid1: TwwDBGrid;
    EDtIni: TEditDate;
    EDtFim: TEditDate;
    SaveDialog1: TSaveDialog;
    JvDBGridExcelExport1: TJvDBGridExcelExport;
    cdsSEMANA: TStringField;
    cdsCLIENTE: TStringField;
    cdsRAZAO_SOCIAL: TStringField;
    cdsPRODUTO: TStringField;
    cdsPROD_CLIENTE: TStringField;
    cdsDESCRICAO: TStringField;
    cdsUM: TStringField;
    cdsPROD_HORA: TFMTBCDField;
    cdsHORA_MAQ: TStringField;
    cdsMOD: TFMTBCDField;
    cdsCCUSTO: TStringField;
    cdsCELULA: TStringField;
    cdsMAQUINA: TStringField;
    cdsSALDO_PA: TFMTBCDField;
    cdsSALDO_AUDIT: TFMTBCDField;
    cdsSEM0: TFMTBCDField;
    cdsS0_HORA_MAQ: TStringField;
    cdsS0_HORA_HOM: TFMTBCDField;
    cdsS0_SALDO: TFMTBCDField;
    cdsSEM1: TFMTBCDField;
    cdsS1_HORA_MAQ: TStringField;
    cdsS1_HORA_HOM: TFMTBCDField;
    cdsS1_SALDO: TFMTBCDField;
    cdsSEM2: TFMTBCDField;
    cdsS2_HORA_MAQ: TStringField;
    cdsS2_HORA_HOM: TFMTBCDField;
    cdsS2_SALDO: TFMTBCDField;
    cdsSEM3: TFMTBCDField;
    cdsS3_HORA_MAQ: TStringField;
    cdsS3_HORA_HOM: TFMTBCDField;
    cdsS3_SALDO: TFMTBCDField;
    cdsSEM4: TFMTBCDField;
    cdsS4_HORA_MAQ: TStringField;
    cdsS4_HORA_HOM: TFMTBCDField;
    cdsS4_SALDO: TFMTBCDField;
    cdsSEM5: TFMTBCDField;
    cdsS5_HORA_MAQ: TStringField;
    cdsS5_HORA_HOM: TFMTBCDField;
    cdsS5_SALDO: TFMTBCDField;
    cdsSEM6: TFMTBCDField;
    cdsS6_HORA_MAQ: TStringField;
    cdsS6_HORA_HOM: TFMTBCDField;
    cdsS6_SALDO: TFMTBCDField;
    cdsFAT_D1: TFMTBCDField;
    cdsFAT_D2: TFMTBCDField;
    cdsFAT_D3: TFMTBCDField;
    cdsFAT_D4: TFMTBCDField;
    cdsFAT_D5: TFMTBCDField;
    procedure btnProcurarClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fRel1: TfRel1;

implementation

uses udm, DateUtils;

{$R *.dfm}

procedure TfRel1.btnProcurarClick(Sender: TObject);
var sem1, sem2, sem3, sem4, sem5 : Integer;
begin
  cds.Close;
  cds.CommandText := 'SELECT  SEMANA, CLIENTE, RAZAO_SOCIAL, PRODUTO, ' +
  ' PROD_CLIENTE, DESCRICAO, UM, PROD_HORA, ATS_VALORES(HORA_MAQ,4) HORA_MAQ, MOD, CCUSTO, CELULA, ' +
  ' MAQUINA, SALDO_PA, SALDO_AUDIT, SEM0, ATS_VALORES(S0_HORA_MAQ,4) S0_HORA_MAQ, S0_HORA_HOM, S0_SALDO, ' +
  ' SEM1, ATS_VALORES(S1_HORA_MAQ,4) S1_HORA_MAQ ,S1_HORA_HOM, S1_SALDO, SEM2, ATS_VALORES(S2_HORA_MAQ,4) S2_HORA_MAQ, S2_HORA_HOM, ' +
  ' S2_SALDO, SEM3, ATS_VALORES(S3_HORA_MAQ,4) S3_HORA_MAQ, S3_HORA_HOM, S3_SALDO, SEM4, ATS_VALORES(S4_HORA_MAQ,4) S4_HORA_MAQ, ' +
  ' S4_HORA_HOM, S4_SALDO, SEM5, ATS_VALORES(S5_HORA_MAQ,4) S5_HORA_MAQ, S5_HORA_HOM, S5_SALDO, SEM6, ATS_VALORES(S6_HORA_MAQ,4) S6_HORA_MAQ, ' +
  ' S6_HORA_HOM, S6_SALDO, FAT_D1, FAT_D2, FAT_D3, FAT_D4, FAT_D5 FROM V_BIPROD';
  SPAcomProd.ParamByName('DTINI').AsString := EDtIni.Text;
  SPAcomProd.ParamByName('DTFIM').AsString := EDtFim.Text;
  SPAcomProd.ExecProc;
  cds.Open;

  sem1 := WeekOfTheYear(EDtIni.Date);

  wwDBGrid1.ColumnByName('SEM0').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S0_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S0_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S0_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';

  sem1 := sem1 + 1;
  if (sem1 > 53) then
    sem1 := 1;

  wwDBGrid1.ColumnByName('SEM1').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S1_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S1_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S1_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';

  sem1 := sem1 + 1;
  if (sem1 > 53) then
    sem1 := 1;

  wwDBGrid1.ColumnByName('SEM2').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S2_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S2_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S2_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';

  sem1 := sem1 + 1;
  if (sem1 > 53) then
    sem1 := 1;

  wwDBGrid1.ColumnByName('SEM3').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S3_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S3_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S3_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';

  sem1 := sem1 + 1;
  if (sem1 > 53) then
    sem1 := 1;

  wwDBGrid1.ColumnByName('SEM4').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S4_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S4_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S4_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';

  sem1 := sem1 + 1;
  if (sem1 > 53) then
    sem1 := 1;

  wwDBGrid1.ColumnByName('SEM5').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S5_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S5_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S5_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';

  sem1 := sem1 + 1;
  if (sem1 > 53) then
    sem1 := 1;

  wwDBGrid1.ColumnByName('SEM6').DisplayLabel := 'Sem-' + intToStr(sem1);
  wwDBGrid1.ColumnByName('S6_HORA_MAQ').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_MAQ';
  wwDBGrid1.ColumnByName('S6_HORA_HOM').DisplayLabel := 'S-' + intToStr(sem1) + '_HORA_HOM';
  wwDBGrid1.ColumnByName('S6_SALDO').DisplayLabel := 'S-' + intToStr(sem1) + '_SALDO';


end;

procedure TfRel1.BitBtn1Click(Sender: TObject);
begin
  if (SaveDialog1.Execute) then
    wwDBGrid1.ExportOptions.FileName := SaveDialog1.FileName;
  wwdbgrid1.ExportOptions.Save;
  MessageDlg('Arquivo exportado com sucesso.',
          mtConfirmation, [mbOk],0);
  {if (SaveDialog1.Execute) then
  begin
    wwDBGrid1.ExportOptions.FileName := SaveDialog1.FileName;
    JvDBGridExcelExport1.FileName := SaveDialog1.FileName;
  end;}
  //wwdbgrid1.ExportOptions.Save;
  //JvDBGridExcelExport1.ExportGrid;
  ShellExecute(Handle, nil, PChar(SaveDialog1.FileName), nil, nil, SW_SHOWNORMAL);
  //MessageDlg('Arquivo exportado com sucesso.',
   //       mtConfirmation, [mbOk],0);}          
end;

end.
